﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

namespace ARelogRelog
{
    public class Utils
    {
        static log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(Form1));

        public static void CloseAllApps()
        {
            CloseProcessByName("Diablo III");
            CloseProcessByName("ARelog");
            CloseProcessByName("Demonbuddy");
        }

        public static void CloseARelogDependingProcesses(ARelog pARelog)
        {
            CloseProcessByExeFileName(pARelog.FileName);

            Thread.Sleep(500);

            foreach (ARelogMonitor pMonitor in pARelog.Monitors)
            {
                if (pMonitor.Enabled)
                {
                    CloseProcessByExeFileName(pMonitor.BuddyPath);
                    CloseProcessByExeFileName(pMonitor.GamePath);
                    Thread.Sleep(1000);
                }
            }
        }

        public static void CloseProcessByName(string n)
        {
            foreach (Process p in Process.GetProcessesByName(n))
            {
                p.Kill();
            }
        }

        public static void CloseProcessByExeFileName(string fn)
        {
            List<string> list = new List<string>();
            list.Add("ARelog");
            list.Add("Diablo III");
            list.Add("Demonbuddy");
            list.Add("Wow");
            list.Add("Honorbuddy");

            foreach (Process p in Process.GetProcesses())
            {
                if (list.Contains(p.ProcessName))
                {
                    try
                    {
                        if (p.MainModule.FileName == fn)
                        {
                            _logger.Debug(string.Format("Killig process [{0}]: " + fn, p.Id));
                            p.Kill();
                        }
                    }
                    catch (Win32Exception ex)
                    {
                        _logger.Warn("Utils.CloseProcessByExeFileName failed accessing Process.MainModule", ex);
                    }
                }
            }
        }

        public static void RunARelog(string aRelogPath)
        {
            if (!Path.IsPathRooted(aRelogPath))
                aRelogPath = Path.Combine(Application.StartupPath, aRelogPath);

            Process p = Process.Start(aRelogPath, "-autostart");
        }

        public static bool IsRunning(ARelog pARelog)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName == "ARelog")
                {
                    try
                    {
                        return p.MainModule.FileName == pARelog.FileName;
                    }
                    catch (Exception ex)
                    {
                        _logger.Warn("Utils.IsRunning failed accessing Process.MainModule", ex);
                    }
                }
            }

            return false;
        }
    }
}
